package cn.wolfcode.crm.Service.impl;


import cn.wolfcode.crm.Service.IEmployeeService;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.EmployeeQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        int i = employeeMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public void insert(Employee record, Long[] ids) {
        //密码加密,使用用户名作为加密的“盐”
        Md5Hash md5Hash = new Md5Hash(record.getPassword(), record.getName().toString());
        record.setPassword(md5Hash.toString());
        employeeMapper.insert(record);
        if (ids !=null&& ids.length >0) {
            for (long id : ids) {
                employeeMapper.insertRealation(record.getId(),id);
            }
        }

    }


    @Override
    public Employee selectByPrimaryKey(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> employeeList = employeeMapper.selectAll();
        return employeeList;
    }

    @Override
    public int updateByPrimaryKey(Employee record) {
        int i = employeeMapper.updateByPrimaryKey(record);

        return i;
    }


    @Override
    public PageInfo<Employee> queryForList(EmployeeQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.queryForList(qo);
        return new PageInfo<>(list);

    }

    @Override
    public Employee isExist(Employee employee) {
        return employeeMapper.isExist(employee);
    }

    @Override
    public List<String> getPermissions(Long id) {

        return employeeMapper.getPermissions(id);
    }

    public Workbook exportXls() {
        //查询所有的员工
        List<Employee> employees = employeeMapper.selectAll();
        //将员工数据写入excel文件中
        //创建工作簿
        HSSFWorkbook book = new HSSFWorkbook();
        //创建纸张
        HSSFSheet sheet = book.createSheet("员工信息");
        //为每个员工对象创建行
        HSSFRow header = sheet.createRow(0);
        header.createCell(0).setCellValue("用户名");
        header.createCell(1).setCellValue("邮箱");
        header.createCell(2).setCellValue("年龄");
        //在行中添加单元格
        for (int i = 0; i < employees.size() ; i++) {
            //从第二行开始建,所以+1
            HSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employees.get(i).getName());
            row.createCell(1).setCellValue(employees.get(i).getEmail());
            row.createCell(2).setCellValue(employees.get(i).getAge());
        }
        //为单元格设置数据
        return book;
    }

    public void importXls(MultipartFile file) throws IOException {
        //创建工作簿
        HSSFWorkbook book = new HSSFWorkbook(file.getInputStream());
        //获取纸张
        HSSFSheet sheet = book.getSheet("员工信息");
        //获取行
        //int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i < sheet.getLastRowNum(); i++) {
            HSSFRow row = sheet.getRow(i + 1);
            String name = row.getCell(0).getStringCellValue();
            String email = row.getCell(1).getStringCellValue();
            int age = Double.valueOf(row.getCell(2).getNumericCellValue()).intValue();

            //将数据保存到数据库
            Employee emp = new Employee();
            emp.setName(name);
            emp.setEmail(email);
            emp.setAge(age);
            emp.setPassword(new Md5Hash("1",name).toString());
        }
    }

    @Override
    public List<Employee> listEmployeesByRoleSns(String... roleSns) {
        return employeeMapper.listEmployeesByRoleSns(roleSns);
    }


    @Override
    public void batchDelete(Long[] ids){
        employeeMapper.batchDelete(ids);
    }

    

}
































